#import "BWCrossStitchPaintbucketTool.h"
#import "BWCrossStitchChangeList.h"

#import "BWGridGeometry.h"
#import "BWThread.h"
#import "BWCrossStitchLayer.h"
#import "BWCrossStitchSet.h"

@implementation BWCrossStitchPaintbucketTool

- (void) seedFillFromRow: (int) row
                  column: (int) column
                  withThread: (BWThread *) thread
{
    int maxColumn = [geometry countAcross] - 1;
    int maxRow = [geometry countDown] - 1;

    if (row < 0 || column < 0 || row > maxRow || column > maxColumn) {
        return;
    }

    if ([changeList isSymbolAtRow: row  column: column]) {
        return;
    }

    if ([[layer crossStitches] isSymbolAtRow: row  column: column]) {
        return;
    }

    [changeList addStitchWithThread: thread
                atRow: row
                column: column];

    [self invalidateRow: row
          column: column];

    // now check neighbors
    [self seedFillFromRow: row - 1
          column: column
          withThread: thread];

    [self seedFillFromRow: row + 1
          column: column
          withThread: thread];

    [self seedFillFromRow: row
          column: column - 1
          withThread: thread];

    [self seedFillFromRow: row
          column: column + 1
          withThread: thread];

} // seedFillFromRow


- (void) startDragAtRow: (int) row  
                 column: (int) column
                 thread: (BWThread *) thread
{
    dropRow = row;
    dropColumn = column;

    [changeList purge];

    [self seedFillFromRow: row
          column: column
          withThread: thread];

} // startDragAtRow


- (void) continueDragAtRow: (int) row
                    column: (int) column
                    thread: (BWThread *) thread
{
    [self invalidateRow: dropRow  column: dropColumn];

    dropRow = row;
    dropColumn = column;

    [changeList purge];

    [self seedFillFromRow: row
          column: column
          withThread: thread];

} // handleMouseDragged


- (NSString *) name
{
    return (@"Fill in areas with cross stitches");
} // name

@end // BWCrossStitchPaintbucketTool

